<template>
    <div>
        <tip :text="textofTip" v-if="showTip" @config="config" @cancel="cancel"></tip>
        <advice v-for="(item,index) in adviceItem" :key="item.id" :advice="item.content" :username="item.realname"
            :id="item.id" :time="parseInt(item.time)" :realname="item.username" @IfDel="IfDel(index)"
            @click.native="showAdviceContent"></advice>
        <seeAdvice ref="writeAdvice" @addAdviceStatus="addAdviceStatus" @noContent="noContent"></seeAdvice>

    </div>
</template>

<script>
    import advice from "../../../components/advice.vue"
    import seeAdvice from "../../advice/seeAdvice.vue"
    import Tip from "../../../components/tips.vue"
    export default {
        components: {
            advice,
            seeAdvice,
            Tip
        },
        props: {
            adviceItem: {
                type: Array,
                default: () => []
            }
        },
        data(){
            return {
                
            }
        },
        methods: {
            noContent() {
                this.msgWay = "warn"
                this.text = "请填写内容"

                this.showMsg = true
                setTimeout(() => {
                    this.showMsg = false
                }, 2000)
            },
            addAdviceStatus(msgWay) {

                if (this.flag) {
                    this.flag = false
                    if (msgWay === "success") {
                        this.msgWay = "success"
                        this.text = "添加成功"
                        this.$refs.writeAdvice.ifSee = false
                        this.$refs.writeAdvice.adviceContent = ""
                        this.showMsg = true
                        setTimeout(() => {
                            this.showMsg = false
                        }, 2000)
                        setTimeout(() => {
                            this.flag = true
                        }, 200)
                    } else {
                        this.text = "添加失败"
                        this.msgWay = "error"
                        this.$refs.writeAdvice.ifSee = false

                        this.showMsg = true
                        setTimeout(() => {
                            this.showMsg = false
                        }, 2000)
                        setTimeout(() => {
                            this.flag = true
                        }, 200)
                    }
                }

            },

            showAdviceContent() {
                this.$refs.writeAdvice.writeAdvice = false
            },
            config() {
                this.$axios.post("/advice/del", {
                    id: this.adviceItem[this.currentDelIndex].id,
                    username: this.$store.getters.user.username
                }).then(res => {
                    console.log(res);

                    if (res.data.error === -1) {
                        this.showTip = false
                        this.msgWay = "success"
                        this.text = "删除成功"
                        this.$refs.writeAdvice.ifSee = false
                        setTimeout(() => {
                            this.showMsg = false
                        }, 2000)
                        this.showMsg = true
                        this.getAllAdvice()
                    } else {
                        this.msgWay = "error"
                        this.text = "删除失败"
                        this.$refs.writeAdvice.ifSee = false

                        setTimeout(() => {
                            this.showMsg = false
                        }, 2000)
                        this.showMsg = true
                        this.showTip = false

                    }
                })
            },
            cancel() {
                this.showTip = false
            },
            IfDel(index) {
                this.currentDelIndex = index
                this.showTip = true
                this.textofTip = "你确定要删除吗"
            },
        },
    }
</script>

<style>

</style>